/*
 * File:   FractalCompute.cpp
 * Author: amouro-m
 *
 * Created on 14 mai 2010, 10:33
 */

#include "tbb/parallel_for.h"

#include <iostream>
#include <sstream>

#include "FractalCompute.hpp"
#include "FractalPrinterNormal.h"
#include "tbb/tick_count.h"
#include "Frame.h"

FractalCompute::FractalCompute(FractalPrinter* fp, ResultPanel* chrono):
_fp(fp),
_result_panel(chrono)
{}

FractalCompute::~FractalCompute() {}

void
FractalCompute::operator() (const tbb::blocked_range2d<unsigned int, unsigned int> & r) const
{
    tbb::blocked_range2d<unsigned int>::row_range_type rows = r.rows();
    tbb::blocked_range2d<unsigned int>::col_range_type cols = r.cols();
    tbb::tick_count start, end;
    start = tbb::tick_count::now();
    for (unsigned int i = rows.begin(); i < rows.end(); ++i)
    {
        for (unsigned int j = cols.begin(); j < cols.end(); ++j)
        {
            _fp->compute(i, j);
        }
    }
    end = tbb::tick_count::now();
    tbb::tick_count::interval_t interval = (end - start);
    std::ostringstream os (std::ostringstream::out);
    os << "Block : " << interval.seconds() << " s";
    std::string string(os.str());

    _result_panel->add_item(string.c_str());
    _fp->repaint();
}

void
FractalCompute::compute(FractalPrinter* fp, ResultPanel* chrono)
{
    chrono->reset();
    tbb::tick_count start, end;
    start = tbb::tick_count::now();
    if(fp->get_fractal() != NULL)
        tbb::parallel_for(tbb::blocked_range2d<unsigned int, unsigned int>(0, fp->get_width(), 0, fp->get_height()), FractalCompute(fp, chrono));
    end = tbb::tick_count::now();
    tbb::tick_count::interval_t interval = (end - start);
    std::ostringstream os (std::ostringstream::out);
    os << "Total : " << interval.seconds() << " s";
    std::string string(os.str());

    chrono->add_item(string.c_str());
}